package com.aishua.fire.common.eenum;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;


/**
 * 白名单接口列表
 * @author Administrator
 *
 */
public enum WhiteInterfaceEnum {

	/** 静态资源访问的接口 **/
	GET_DOCS_JSON("/v2/api-docs","GET","获取文档json串接口"),
	SWAGGER("/dist/{1}","GET","进入swagger页面接口"),
	BATCH_TEMPLATE("/batchTemplate/{1}","GET","excel模板文件地址访问接口"),
	phoneWEB("/phoneweb/{1}","GET","前端页面"),
	backstageWEB("/backstageweb/{1}","GET","后台页面"),
	phoneDriverWEB("/driverweb/{1}","GET","司机微信模块页面"),
	keyFile("/MP_verify_0GjiDD9sudr8Iu1V.txt","GET","微信认证文件"),

	/** 接口初始化相关功能的接口 **/
	initPermTable("/initPermTable","GET","初始化权限相关表接口"),
	updatePermTable("/updatePermTable","GET","更新权限表接口"),
	
	/** 配置类接口 **/
	PIC("/images/{1}/{2}","GET","获取图片"),
	PIC1("/images/plan/{1}/{2}","GET","获取图片"),
	getLoginCode("/getLoginCode","GET","获取验证码"),
	Login("/login","POST","登陆"),
	appLogin("/appLogin","POST","app登陆"),
	FILE("/file","GET","获取文件");
	

	/** 接口名 **/
	private String interfaceName;
	/** 方法名 **/
	private String methodName;
	/** 描述 **/
	private String desc;
	
	WhiteInterfaceEnum(String interfaceName, String methodName, String desc){
		this.interfaceName = interfaceName;
		this.methodName = methodName;
		this.desc = desc;
	}

	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public static boolean isInWriteList(HttpServletRequest request) {
		String method = request.getMethod();
		//如果是options请求,默认返回true
		if("options".equalsIgnoreCase(method)) {
			return true;
		}
		
//		log.info("当前请求的方法名:{},",method);
		String uriVal = request.getRequestURI();
//		log.info("当前请求的uriVal:{}+{}",uriVal,method);
		for(WhiteInterfaceEnum writeEnum : values()) {
			String reqMehod = writeEnum.getMethodName();
//			log.info("遍历的方法名:{}",reqMehod);
			if(reqMehod.equalsIgnoreCase(method)) {
				String reqUrl = writeEnum.getInterfaceName();
//				log.info("遍历相同的method的uri:{},",reqUrl);
				/*if(reqUrl.equals(uriVal)) {
					return true;
				}*/
				String regex = "\\{[^\\}]+\\}";
				String uriRegex = reqUrl.replaceAll(regex, "[^\\\\}]+");
				if(Pattern.matches(uriRegex, uriVal)) {
					return true;
				}
			}
		}
		return false;
	}

	
}
